/**
 *====================================================
 * 文件名称: BulkstockManagementCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月26日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.project.action.v1.model.output.BadassetsHandleDetail;
import com.yuexunit.finance.project.action.v1.model.output.BadassetsHandleLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.BadassetsInventory;
import com.yuexunit.finance.project.action.v1.model.output.ReturnFinanceBulkstock;
import com.yuexunit.finance.project.action.v1.model.output.ReturnFinanceDetail;
import com.yuexunit.finance.project.action.v1.model.output.ReturnFinanceLineInfo;
import com.yuexunit.finance.project.entity.BadassetsHandleEntity;
import com.yuexunit.finance.project.entity.BadassetsInventoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceReturnBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceReturnEntity;
import com.yuexunit.finance.project.service.BadassetsInventoryService;
import com.yuexunit.finance.project.service.ContainerInventoryService;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceBulkstockService;
import com.yuexunit.finance.project.service.FinanceReturnBulkstockService;
import com.yuexunit.inject.ComponentFactory;

/**
 * @ClassName: BulkstockManagementCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月26日 下午10:27:38
 */
public class BulkstockManagementCastor {

	/** 生成退货信息 */
	public static FinanceReturnEntity fromFinanceReturn(Long financeId, BigDecimal deductionAmount, String remark) {
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);

		FinanceReturnEntity entity = new FinanceReturnEntity();
		entity.setFinanceId(financeId);
		entity.setDeductionAmount(deductionAmount);
		entity.setRemark(remark);
		entity.setFinanceId(financeId);
		FinanceApplicationEntity financeEntity = financeApplicationService.inquireFinanceApplicationEntity(financeId);
		entity.setFinanceNumber(financeEntity.getFinanceNumber());
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		return entity;
	}

	/** 退货货物信息 */
	public static List<FinanceReturnBulkstockEntity> fromFinanceReturnBulkstock(Long retunId, Long[] bulkstockIds, BigDecimal[] quantitys, String[] units) {
		List<FinanceReturnBulkstockEntity> returnBulkstockList = new ArrayList<>();
		for (int i = 0; i < bulkstockIds.length; i++) {
			FinanceReturnBulkstockEntity returnBulkstockEntity = new FinanceReturnBulkstockEntity();
			returnBulkstockEntity.setReturnId(retunId);
			returnBulkstockEntity.setBulkstockId(bulkstockIds[i]);
			returnBulkstockEntity.setQuantity(quantitys[i]);
			returnBulkstockEntity.setUnit(units[i]);
			returnBulkstockList.add(returnBulkstockEntity);
		}
		return returnBulkstockList;
	}

	public static ReturnFinanceDetail toReturnFinanceDetail(FinanceReturnEntity entity) {
		ReturnFinanceDetail detail = new ReturnFinanceDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setDeductionAmount(entity.getDeductionAmount().toString());

		FinanceReturnBulkstockService financeReturnBulkstockService = ComponentFactory.loadClass(FinanceReturnBulkstockService.class);
		FinanceBulkstockService financeBulkstockService = ComponentFactory.loadClass(FinanceBulkstockService.class);
		List<FinanceReturnBulkstockEntity> returnBulkstockList = financeReturnBulkstockService.inquireReturnFinanceBulkstockList(entity.getReturnId());
		for (FinanceReturnBulkstockEntity rc : returnBulkstockList) {
			ReturnFinanceBulkstock bulkstock = new ReturnFinanceBulkstock();
			BeanUtils.copyProperties(rc, bulkstock);
			FinanceBulkstockEntity c = financeBulkstockService.inquireFinanceBulkstock(rc.getBulkstockId());
			BeanUtils.copyProperties(c, bulkstock);
			detail.getBulkstocks().add(bulkstock);
		}
		return detail;
	}

	public static ReturnFinanceLineInfo toReturnFinanceLineInfo(FinanceReturnEntity entity) {
		ReturnFinanceLineInfo info = new ReturnFinanceLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setDeductionAmount(entity.getDeductionAmount().toString());
		return info;
	}

	// =================================================================================================================================================//
	public static BadassetsHandleDetail toBadassetsHandleDetail(BadassetsHandleEntity entity) {
		BadassetsHandleDetail detail = new BadassetsHandleDetail();
		BeanUtils.copyProperties(entity, detail);

		BadassetsInventoryService badassetsInventoryService = ComponentFactory.loadClass(BadassetsInventoryService.class);
		List<BadassetsInventoryEntity> inventoryList = badassetsInventoryService.inquireHandleBadassetsInventoryList(entity.getBadassetsHandleId());
		ContainerInventoryService bulkstockInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);
		for (BadassetsInventoryEntity bie : inventoryList) {
			BadassetsInventory bi = new BadassetsInventory();
			bi.setBadassetsHandleId(bie.getBadassetsHandleId());
			bi.setBadassetsInventoryId(bie.getBadassetsInventoryId());

			ContainerInventoryEntity cie = bulkstockInventoryService.inquireContainerInventory(bie.getInventoryId());
			bi.setInventory(BulkstockCastor.toContainerInventory(cie, Boolean.FALSE));

			detail.getBadassetsInventories().add(bi);
		}
		return detail;
	}

	public static BadassetsHandleLineInfo toBadassetsHandleLineInfo(BadassetsHandleEntity entity) {
		BadassetsHandleLineInfo info = new BadassetsHandleLineInfo();
		BeanUtils.copyProperties(entity, info);
		return info;
	}

}
